#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include"Log.hpp"

// 将套接字结构体内的网络字节序转化为主机序列，ip转换为字符串
class InetAddr
{
    void GetAddress(struct sockaddr_in addr)
    {
        _ip = inet_ntoa(addr.sin_addr);
        _port = ntohs(addr.sin_port);
    }

public:
    // 提供两种初始化方式
    InetAddr(struct sockaddr_in &addr)
        : _addr(addr)
    {
        //Log(DEBUG,"InetAddr....");
        GetAddress(addr);
    }
    InetAddr(uint16_t port,std::string ip="0")
        : _ip(ip), _port(port)
    {
        //Log(DEBUG,"InetAddr....");
        _addr.sin_family = AF_INET;
        _addr.sin_addr.s_addr = inet_addr(ip.c_str());
        _addr.sin_port = htons(port);
        
    }
    InetAddr()
    {
        //Log(DEBUG,"InetAddr....");
    }
    struct sockaddr_in Addr()
    {
        return _addr;
    }
    std::string IP()
    {
        return _ip;
    }
    uint16_t port()
    {
        return _port;
    }

private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};